package com.aiowang.springboot.single.activemq.starter;

import com.aiowang.springboot.single.activemq.config.CustomProperties;
import com.aiowang.springboot.single.activemq.listener.ActiveMQTransportListener;
import com.aiowang.springboot.single.activemq.utils.SingleActiveMQApplicationContextUtils;
import com.aiowang.springboot.single.activemq.utils.SingleActiveMQUtils;
import com.aiowang.springboot.single.activemq.utils.ToolsUtils;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsMessagingTemplate;

import javax.jms.ConnectionFactory;

/**
 * @author aiowang
 * @date 2020-03-30 20:59
 */
@ConditionalOnProperty(value = "spring.activemq.custom.brokerUrl", matchIfMissing=false)
@Configuration
@EnableConfigurationProperties({CustomProperties.class})
public class SingleActivemqAutoConfigure {

    @Autowired
    private CustomProperties customProperties;


    /**
     * 连接对象，使用CachingConnectionFactory，SingleConnectionFactory产生唯一的客户端
     *
     * @return 连接对象
     */
    @Bean(name = "customConnectionFactory")
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "spring.activemq.custom", value = "single", havingValue = "true")
    public CachingConnectionFactory customConnectionFactorySingleCache() {
        ActiveMQConnectionFactory factory = ToolsUtils.getActiveMQConnectionFactory(customProperties);
        factory.setTransportListener(new ActiveMQTransportListener("custom"));
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setTargetConnectionFactory(factory);
        return cachingConnectionFactory;
    }

    /**
     * 连接对象，使用CachingConnectionFactory，SingleConnectionFactory产生唯一的客户端
     *
     * @return 连接对象
     */
    @Bean(name = "customConnectionFactory")
    @ConditionalOnMissingBean
    public ActiveMQConnectionFactory customConnectionFactorySingle() {
        ActiveMQConnectionFactory factory = ToolsUtils.getActiveMQConnectionFactory(customProperties);
        return factory;
    }


    /**
     * 消息生成者
     *
     * @param customConnectionFactory 连接对象
     * @return jmsMessagingTemplate
     */

    @Bean(name = "customActivemqTemplate")
    public JmsMessagingTemplate customActivemqTemplate(
            @Autowired @Qualifier("customConnectionFactory") ConnectionFactory customConnectionFactory) {
        JmsMessagingTemplate template = new JmsMessagingTemplate(customConnectionFactory);
        return template;
    }

    /**
     * 接收队列消息,使用JmsListener注解时，containerFactory = "innerTopicFactory"
     *
     * @param connectionFactory 连接对象
     * @return jmsListenerContainerFactory
     */
    @Bean(name = "customTopicFactory")
    public JmsListenerContainerFactory customTopicFactory(
            @Autowired @Qualifier("customConnectionFactory") ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }

    /**
     * 接收队列消息,使用JmsListener注解时，containerFactory = "innerQueueFactory"
     *
     * @param connectionFactory 连接对象
     * @return jmsListenerContainerFactory
     */
    @Bean(name = "customQueueFactory")
    public JmsListenerContainerFactory customQueueFactory(
            @Autowired @Qualifier("customConnectionFactory") ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(false);
        return factory;
    }



    /**
     * JmsMessagingTemplate 消息模板
     */
    @Autowired
    private JmsMessagingTemplate customActivemqTemplate;

    /**
     * 注入 ActivemqUtil类
     * @return ActivemqUtil
     */
    @Bean
    @ConditionalOnMissingBean(SingleActiveMQUtils.class)
    public SingleActiveMQUtils getPhoneService(){
        return new SingleActiveMQUtils(customActivemqTemplate);
    }

    /**
     * 注入DynamicActiveMQApplicationContextUtils
     * @return DynamicActiveMQApplicationContextUtils
     */
    @Bean
    @ConditionalOnMissingBean(SingleActiveMQApplicationContextUtils.class)
    public SingleActiveMQApplicationContextUtils getServiceFactory(){
        return new SingleActiveMQApplicationContextUtils();
    }
}
